package com.hyt.it.ogt.kq.service.gov.schedule.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.hyt.it.ogt.kq.service.gov.schedule.impl.ExamRoomScheduleImpl.PackageExamRoomCandidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.Candidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ExamRoom;

public class SupportUtils {

    public static int countVirCandidateSubject(Map<String, List<String>> virId2ExamSubjectsMap, String virCandidateID) {
        int count = 0;
        for (String examSubject : virId2ExamSubjectsMap.get(virCandidateID)) {
            if (!examSubject.equalsIgnoreCase("ANY")) {
                count++;
            }
        }
        return count;
    }

    public static String mergeVirCandidateSubject(Map<String, List<String>> virId2ExamSubjectsMap,
            String virCandidateID) {
        String mergeString = virId2ExamSubjectsMap.get(virCandidateID).stream()
                .filter(item -> !item.equalsIgnoreCase("ANY"))
                .collect(Collectors.joining(","));
        return mergeString;
    }

    public static Map<String, Map<String, List<List<Candidate>>>> transform(
            Map<String, Map<String, PackageExamRoomCandidate>> packageExamRoomCandidate) {
        Map<String, Map<String, List<List<Candidate>>>> result = new HashMap<>();
        for (Map.Entry<String, Map<String, PackageExamRoomCandidate>> entry : packageExamRoomCandidate
                .entrySet()) {
            String batchNO = entry.getKey();
            Map<String, PackageExamRoomCandidate> examRoomCandidateMap = entry.getValue();
            examRoomCandidateMap.entrySet().forEach(item -> {
                String groupName = item.getKey();
                List<List<Candidate>> candidateList = item.getValue().getCandidates();
                if (!result.containsKey(groupName)) {
                    result.put(groupName, new HashMap<>());
                }
                result.get(groupName).put(batchNO, candidateList);
            });
        }
        return result;
    }

    public static Map<String, List<PackageExamRoomCandidate>> extractPackage(
            Map<String, Map<String, PackageExamRoomCandidate>> packageExamRoomCandidate) {
        Map<String, List<PackageExamRoomCandidate>> result = new HashMap<>();
        for (Map.Entry<String, Map<String, PackageExamRoomCandidate>> entry : packageExamRoomCandidate.entrySet()) {
            Map<String, PackageExamRoomCandidate> examRoomCandidateMap = entry.getValue();
            examRoomCandidateMap.entrySet().forEach(item -> {
                String groupName = item.getKey();
                PackageExamRoomCandidate pack = item.getValue();
                if (!result.containsKey(groupName)) {
                    result.put(groupName, new ArrayList<>());
                }
                result.get(groupName).add(pack);
            });
        }
        return result;
    }

    public static Map<String, List<ExamRoom>> extractPackageRooms(
            Map<String, Map<String, PackageExamRoomCandidate>> packageExamRoomCandidate) {
        Map<String, List<PackageExamRoomCandidate>> packageExamRoomCandidateMap = extractPackage(
                packageExamRoomCandidate);
        Map<String, List<ExamRoom>> result = new HashMap<>();
        // 按组合开始进行编排
        for (Map.Entry<String, List<PackageExamRoomCandidate>> entry : packageExamRoomCandidateMap.entrySet()) {
            String groupName = entry.getKey();
            Set<ExamRoom> examRoomSet = new HashSet<>();
            entry.getValue().forEach(item -> {
                examRoomSet.addAll(item.getExamRooms());
            });
            result.put(groupName, new ArrayList<>(examRoomSet));
        }
        return result;

    }

    public static Map<String, List<Candidate>> flatMap(Map<String, List<List<Candidate>>> batchCandidates) {
        Map<String, List<Candidate>> result = new HashMap<>();
        for (Map.Entry<String, List<List<Candidate>>> entry : batchCandidates.entrySet()) {
            String batchNO = entry.getKey();
            List<List<Candidate>> candidateList = entry.getValue();
            List<Candidate> candidates = new ArrayList<>();
            candidateList.forEach(item -> candidates.addAll(item));
            result.put(batchNO, candidates);
        }
        return result;
    }

    public static Map<String, List<Candidate>> sortByCandidateNumber(Map<String, List<Candidate>> batchCandidates) {
        List<Map.Entry<String, List<Candidate>>> batchCandidateList = new ArrayList<Map.Entry<String, List<Candidate>>>(
                batchCandidates.entrySet());
        Collections.sort(batchCandidateList, new Comparator<Map.Entry<String, List<Candidate>>>() {
            public int compare(Map.Entry<String, List<Candidate>> o1, Map.Entry<String, List<Candidate>> o2) {
                return (o2.getValue().size() - o1.getValue().size());
            }
        });

        Map<String, List<Candidate>> sortedBatchCandidateMap = new LinkedHashMap<String, List<Candidate>>();
        for (Map.Entry<String, List<Candidate>> item : batchCandidateList) {
            sortedBatchCandidateMap.put(item.getKey(), item.getValue());
        }
        return sortedBatchCandidateMap;
    }

}
